package com.lingnan.controller;

import com.lingnan.dto.AddFavoriteRequest;
import com.lingnan.dto.BatchRemoveFavoriteRequest;
import com.lingnan.dto.RemoveFavoriteRequest;
import com.lingnan.entity.vo.Result;
import com.lingnan.service.JobFavoriteService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/job/job-favorite")
@Slf4j
public class JobFavoriteController {
    
    @Autowired
    private JobFavoriteService jobFavoriteService;
    
    /**
     * 添加收藏
     */
    @PostMapping("/add")
    public Result addFavorite(@RequestBody @Valid AddFavoriteRequest request) {
        try {
            log.info("添加职位收藏：{}", request);
            jobFavoriteService.addFavorite(request.getSeekerId(), request.getPositionId());
        } catch (Exception e) {
            log.error("添加职位收藏失败：{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
        return Result.ok("收藏成功");
    }
    
    /**
     * 取消收藏
     */
    @DeleteMapping("/remove")
    public Result removeFavorite(@RequestBody @Valid RemoveFavoriteRequest request) {
        try {
            log.info("取消职位收藏：{}", request);
            jobFavoriteService.removeFavorite(request.getSeekerId(), request.getPositionId());
        } catch (Exception e) {
            log.error("取消职位收藏失败：{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
        return Result.ok("取消收藏成功");
    }
    
    /**
     * 检查收藏状态
     */
    @GetMapping("/check")
    public Result checkFavoriteStatus(@RequestParam(value = "seekerId") Long seekerId, @RequestParam(value = "positionId") Long positionId) {
        try {
            boolean isFavorited = jobFavoriteService.isFavorited(seekerId, positionId);
            return Result.ok(isFavorited);
        } catch (Exception e) {
            log.error("查询收藏状态失败：{}", e.getMessage(), e);
            return Result.fail("查询收藏状态失败");
        }
    }
    
    /**
     * 获取收藏列表
     */
    @GetMapping("/list")
    public Result getFavoriteList(@RequestParam(value = "seekerId") Long seekerId) {
        try {
            List<Map<String, Object>> favoriteList = jobFavoriteService.getFavoriteList(seekerId);
            return Result.ok( favoriteList);
        } catch (Exception e) {
            log.error("查询收藏列表失败：{}", e.getMessage(), e);
            return Result.fail("查询收藏列表失败");
        }
    }
    
    /**
     * 批量取消收藏
     */
    @DeleteMapping("/batch-remove")
    public Result batchRemoveFavorites(@RequestBody @Valid BatchRemoveFavoriteRequest request) {
        try {
            log.info("批量取消职位收藏：{}", request);
            jobFavoriteService.batchRemoveFavorites(request.getSeekerId(), request.getPositionIds());
        } catch (Exception e) {
            log.error("批量取消职位收藏失败：{}", e.getMessage(), e);
            return Result.fail("批量取消收藏失败");
        }
        return Result.ok("批量取消收藏成功");
    }
}